import {
  getToken
} from "../../../utils/getToken"
import {
  qqmap
} from '../../../utils/qqmap'

Page({
  data: {
    repairDetail: {},
    // 天安门
    latitude: 39.908823,
    longitude: 116.39747,
    // 标记点
    markers: [{
      id: 1,
      // 天安门
      latitude: 39.908823,
      longitude: 116.39747,
      width: 40,
      height: 40,
      iconPath: '/static/images/marker.png'
    }, {
      id: 2,
      // 端门
      latitude: 39.91035,
      longitude: 116.397432,
      width: 40,
      height: 40,
      // 可以是网络图片
      iconPath: '/static/images/marker.png'
    }],
    // 路线数据
    polyline: []
  },
  onLoad(options) {
    console.log('保修详情页面接收到的数据', options)
    this.getRepairDetail(options.id)
    this.getPolyline()
  },
  getPolyline() {
    qqmap.direction({
      // 路线规划方式
      mode: 'bicycling',
      // 路线规划起点
      from: '39.908823,116.39747',
      // 路线规划终点
      to: '39.91035,116.397432',
      success: (res) => {
        console.log('路线规划数据', res)
        const coors = res.result.routes[0].polyline
        const pl = [];
        // 坐标解压算法：https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/methodDirection#4
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / 1000000;
        }
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({
            latitude: coors[i],
            longitude: coors[i + 1]
          })
        }
        console.log('coors', coors)
        console.log('pl', pl)
        this.setData({
          polyline: [{
            points: pl,
            points: pl,
            width: 6,
            borderColor: '#2f693c',
            borderWidth: 1
          }]
        })
      }
    })
  },
  // 获取房屋详情
  getRepairDetail(id) {
    wx.request({
      url: `https://live-api.itheima.net/repair/${id}`,
      method: 'GET',
      header: {
        Authorization: 'Bearer ' + getToken()
      },
      success: (res) => {
        console.log('房屋详情success', res)
        if (res.data.code !== 10000) {
          return wx.showToast({
            title: res.data.message,
          })
        }
        this.setData({
          repairDetail: res.data.data
        })
      }
    })
  }
});